package pack1;

import java.io.IOException;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swim.entity.Skill;
import com.swim.session.RegisteredUserRemote;
import com.swim.session.SkillRemote;

public class RegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public RegistrationServlet() {
        super();
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = "/WEB-INF/noauth/registration.jsp";
		Object auth = request.getSession().getAttribute("authenticated");
		if(auth != null && (boolean)auth )
			url = "index.jsp";
		else {
			InitialContext jndiContext;
			try {
				jndiContext = (InitialContext) Util.getInitialContext();
				Object ref = jndiContext.lookup("SkillBeanJNDI");
				SkillRemote r = (SkillRemote) ref;
				
				List<Skill> skills = r.getSkills();
				if(skills.size()==0) {
					request.setAttribute("title","The website is not configured yet!");
					request.setAttribute("message","<p>You can't register yet, you have to wait for the administrator to configure the website.</p>");
					RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
					dispatcher.forward(request, response); 
					return;
				}
				request.setAttribute("skills", skills);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher(url);
		dispatcher.forward(request, response); 
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{		
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			String name = request.getParameter("name");
			String surname = request.getParameter("surname");
			String city = request.getParameter("city");
			
			if(email == null || password == null || name == null || surname == null || city == null 
					|| email.equals("") || password.equals("")|| name.equals("")|| surname.equals("")|| city.equals("")) {
				throw new Exception();
			} else {
				
				response.setContentType("text/html");

				if(email == null || email.length() <= 0) {
					throw new Exception();
				}
					InitialContext jndiContext = (InitialContext) Util.getInitialContext();
					Object ref = jndiContext.lookup("RegisteredUserBeanJNDI");
					RegisteredUserRemote r = (RegisteredUserRemote) ref;

					String[] skills = request.getParameterValues("checkboxes");
					boolean user = false;
					if(skills!=null && skills.length != 0){
						user = r.addUser(email, password, name, surname, city);
						r.addSkill(skills,email);
					} else
						throw new Exception();
					
					if(!user) {
						throw new Exception();
					}

					request.setAttribute("title","Registration successful!");
					request.setAttribute("message","<p>You are now registered.</p>" +
							"<p>Go to the <a href=\"login.jsp\">login</a> page to login or <a href=\"index.jsp\">home</a> to browse the site as an unregistered user.</p>");
					RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
					dispatcher.forward(request, response);

			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("title","Error registering");
			request.setAttribute("message","<p>There was an error during registration.</p>" +
					"<p>Go to the <a href=\"registration.jsp\">registration</a> page to register or <a href=\"index.jsp\">home</a>.</p>");
			RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/target.jsp");
			dispatcher.forward(request, response); 
		}
	}
	
}
